package com.hrt.manage.sncode.dao.impl;


import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.SnCodeSequence;
import com.hrt.manage.sncode.dao.SNcodeDao;

@Repository
public class SNcodeDaoImpl extends BaseDaoImpl<SnCodeSequence> implements SNcodeDao {

	@Override
	public SnCodeSequence getSnCodeByCode(String snCode) {
		final String hql = "select sn from SnCodeSequence sn where sn.snCode='"+snCode+"' and sn.isdelete=0 and sn.status=0 and sn.useTime is null";
		return 	getHibernateTemplate().execute(new HibernateCallback<SnCodeSequence>(){

			@Override
			public SnCodeSequence doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query=session.createQuery(hql);
				SnCodeSequence snCode=(SnCodeSequence) query.uniqueResult();
				return snCode;
			}
			
		});
		
	}

	
	
	@Override
	public SnCodeSequence getSnCodeByCode(String snCode, Integer reType) {
		final String hql = "select sn from SnCodeSequence sn where sn.snCode='"+snCode+"' and sn.isdelete=0 and sn.status=0 and sn.useTime is null and sn.reType="+reType;
		return 	getHibernateTemplate().execute(new HibernateCallback<SnCodeSequence>(){

			@Override
			public SnCodeSequence doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query=session.createQuery(hql);
				SnCodeSequence snCode=(SnCodeSequence) query.uniqueResult();
				return snCode;
			}
			
		});
	}



	@Override
	public List<SnCodeSequence> querySnCodeByStockId(Long stockId) {
		final String hql = "select sn from SnCodeSequence sn where sn.useStockId='"+stockId+"' and sn.isdelete=0 and sn.status=1 and sn.useTime is not null";
		return 	getHibernateTemplate().execute(new HibernateCallback<List<SnCodeSequence>>(){

			@Override
			public List<SnCodeSequence> doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query=session.createQuery(hql);		
				return query.list();
			}
			
		});
	}



	

}
